#include "base_view_model.h"
#include <QDebug>

namespace presentation {

BaseViewModel::BaseViewModel(QObject* parent)
    : QObject(parent)
    , m_delayedInitTimer(new QTimer(this))
{
    // 设置延迟初始化定时器
    m_delayedInitTimer->setSingleShot(true);
    connect(m_delayedInitTimer, &QTimer::timeout, this, &BaseViewModel::onDelayedInitialize);
    
    qDebug() << "BaseViewModel: Created" << metaObject()->className();
}

void BaseViewModel::initialize()
{
    if (m_isInitialized) {
        qDebug() << "BaseViewModel: Already initialized" << metaObject()->className();
        return;
    }

    qDebug() << "BaseViewModel: Initializing" << metaObject()->className();
    setLoading(true);
    
    try {
        // 子类可以重写此方法来实现特定的初始化逻辑
        // 这里只设置基本状态
        setInitialized(true);
        setLoading(false);
        
        qDebug() << "BaseViewModel: Successfully initialized" << metaObject()->className();
    } catch (const std::exception& e) {
        setLoading(false);
        handleError(QString("Initialization failed: %1").arg(e.what()));
    }
}

void BaseViewModel::cleanup()
{
    if (!m_isInitialized) {
        return;
    }

    qDebug() << "BaseViewModel: Cleaning up" << metaObject()->className();
    
    // 停止延迟初始化定时器
    if (m_delayedInitTimer->isActive()) {
        m_delayedInitTimer->stop();
    }
    
    // 子类可以重写此方法来实现特定的清理逻辑
    setInitialized(false);
    clearError();
    
    qDebug() << "BaseViewModel: Cleanup completed" << metaObject()->className();
}

void BaseViewModel::handleError(const QString& error)
{
    qWarning() << "BaseViewModel: Error in" << metaObject()->className() << ":" << error;
    
    m_lastError = error;
    setLoading(false);
    
    emit errorOccurred(error);
}

void BaseViewModel::setLoading(bool loading)
{
    if (m_isLoading != loading) {
        m_isLoading = loading;
        emit loadingChanged();
        
        if (loading) {
            qDebug() << "BaseViewModel: Started loading" << metaObject()->className();
        } else {
            qDebug() << "BaseViewModel: Finished loading" << metaObject()->className();
        }
    }
}

void BaseViewModel::clearError()
{
    if (!m_lastError.isEmpty()) {
        m_lastError.clear();
        emit errorOccurred(QString()); // 发送空错误表示清除
    }
}

void BaseViewModel::setInitialized(bool initialized)
{
    if (m_isInitialized != initialized) {
        m_isInitialized = initialized;
        emit initializedChanged();
        
        if (initialized) {
            qDebug() << "BaseViewModel: Marked as initialized" << metaObject()->className();
        } else {
            qDebug() << "BaseViewModel: Marked as uninitialized" << metaObject()->className();
        }
    }
}

void BaseViewModel::delayedInitialize(int delayMs)
{
    if (m_delayedInitTimer->isActive()) {
        m_delayedInitTimer->stop();
    }
    
    m_delayedInitTimer->setInterval(delayMs);
    m_delayedInitTimer->start();
    
    qDebug() << "BaseViewModel: Scheduled delayed initialization in" << delayMs << "ms" << metaObject()->className();
}

void BaseViewModel::onDelayedInitialize()
{
    qDebug() << "BaseViewModel: Executing delayed initialization" << metaObject()->className();
    initialize();
}

} // namespace presentation
